#!/usr/bin/env python
# encoding: utf-8
"""
    @author:水痕
    @time:2018-09-04 14:45:52
    @email:332904234@qq.com
    @version:1.0
    @fileName:forms.py
    @describe:
"""

from django import forms
from utils.captcha import Captcha


class RegisterForm(forms.Form):
    """
    用户注册的form校验
    """
    username = forms.CharField(required=True, error_messages={'required': '用户名不能为空'})
    password = forms.CharField(required=True, min_length=5,
                               error_messages={'required': '密码不能为空', 'min_length': '密码长度不能小于5个字符'})
    # 验证码的字段
    captcha = forms.CharField(max_length=4, min_length=4)

    def clean_captcha(self):
        # 获取用户输入的验证码
        captcha = self.cleaned_data.get('captcha', None)
        if Captcha.check_captcha(captcha=captcha):
            return captcha
        else:
            raise forms.ValidationError('验证码错误')


class LoginForm(forms.Form):
    """
    用户登录的form校验
    """
    username = forms.CharField(required=True, error_messages={'required': '用户名不能为空'})
    password = forms.CharField(required=True, error_messages={'required': '密码不能为空'})

